var WebSocket = require('ws');
var url = require('url')


function wstest(server){
    const wss = new WebSocket.Server({
        server: server
    });
    //var timer;
    // var db = new MongoClient(config["mongo"]);
    // var connection = db.connect();
    wss.on('connection', (ws, req) => {
        const location = url.parse(req.url, true);
        ws.path = location.path;
        console.log(location.path)
        //connection is up, let's add a simple simple event
        ws.on('message', (message) => {
            console.log(message)
            try {
                console.log(JSON.parse(message))
            } catch (error) {
                console.log(error)
            }
            //log the received message and send it back to the client
        });
        var timer = setInterval(() => {
            //console.log(server_status)
            ws.send(JSON.stringify(new Date().getTime()))
        }, 1000);
    });
}

module.exports = wstest;